﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KaytechSoftware.Cms.App.Areas.Admin.Models.Entities;
using KaytechSoftware.Cms.App.Areas.Admin.Models.Filters;
using KaytechSoftware.Cms.App.Common;
using KaytechSoftware.Cms.Services.Dto;
using KaytechSoftware.Cms.Services.Interfaces;
using KaytechSoftware.Cms.Services.Interfaces.Entity;

namespace KaytechSoftware.Cms.App.Areas.Admin.Controllers
{
    public class PhotoController : GridController<PhotoFiltersModel, PhotoModel, PhotoDto>
    {
        private readonly IPhotoService _photoService;
        private readonly IPhotoAlbumService _photoAlbumService;

        public PhotoController(IPhotoService photoService, IPhotoAlbumService photoAlbumService) : base(photoService)
        {
            _photoService = photoService;
            _photoAlbumService = photoAlbumService;
        }

        public override ActionResult Add()
        {
            var albumDtos = _photoAlbumService.GetAll();
            var model = CreateEntityModel();
            model.RegisterCollection(CollectionKey.PhotoAlbum, albumDtos, p => p.Id, p => p.Title);

            var selectedPhotoAlbumId = Session[SessionKeys.SelectedPhotoAlbum] as long?;
            if (selectedPhotoAlbumId.HasValue)
                model.PhotoAlbumId = selectedPhotoAlbumId.Value;

            return Details(model);
        }

        public override ActionResult Edit(long id)
        {
            var photoDto = _photoService.GetDtoById(id);
            var albumDtos = _photoAlbumService.GetAll();

            var model = CreateEntityModel();
            model.FromDto(photoDto);
            model.RegisterCollection(CollectionKey.PhotoAlbum, albumDtos, p => p.Id, p => p.Title);

            return Details(model);
        }

        public override ActionResult Delete(long id)
        {
            return base.Delete(id);
        }

        public override void OnPreSave(PhotoModel editModel)
        {
            Session[SessionKeys.SelectedPhotoAlbum] = editModel.PhotoAlbumId;
        }

        public override PhotoFiltersModel RestoreOrGetDefaultFilters()
        {
            var filters = new PhotoFiltersModel();
            var selectedPhotoAlbumId = Session[SessionKeys.SelectedPhotoAlbum] as long?;
            if (selectedPhotoAlbumId.HasValue)
            {
                filters.SelectedPhotoAlbumId = selectedPhotoAlbumId.Value;
            }
            else
            {
                var photoAlbumDtos = _photoAlbumService.GetAll();
                if (photoAlbumDtos.Any())
                    filters.SelectedPhotoAlbumId = photoAlbumDtos.OrderBy(a => a.Id).First().Id;
            }

            RegisterCollectionsForFilters(filters);
            return filters;
        }

        public override void RegisterCollectionsForFilters(PhotoFiltersModel filters)
        {
            var photoAlbumDtos = _photoAlbumService.GetAll();
            filters.RegisterCollection(CollectionKey.PhotoAlbum, photoAlbumDtos, a => a.Id, a => a.Title);
            Session[SessionKeys.SelectedPhotoAlbum] = filters.SelectedPhotoAlbumId;
        }

        protected override IList<PhotoDto> Filter(PhotoFiltersModel filtersModel)
        {
            var items = _photoService.GetAllFromAlbum(filtersModel.SelectedPhotoAlbumId);
            return items;
        }
    }
}
